package com.db.server;

import com.db.server.task.TaskQueue;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private int port;
    private ServerSession serverSession;

    public Server(int port, ServerSession session) {
        this.port = port;
        this.serverSession = session;
    }

    public void start() {
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            while (true) {
                Socket socket = serverSocket.accept();
                RequestReceiver newClient = new RequestReceiver(socket, serverSession, new TaskQueue());
                serverSession.addClient(newClient);
                newClient.start();
            }
        } catch (IOException e) {
            System.err.println("Cannot get sockets");
        }
    }
}
